package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

/**
 * @author : Sonic
 * @version : 1.0
 * @createTime : 2024-12-08  14:34
 * @description : 数据统计，Echarts动态展示
 */
@RestController
@RequestMapping("/admin/report")
@Slf4j
@Api(tags = "数据统计")
public class ReportController {
    @Autowired
    private ReportService reportService;

    /**
     * @param: [begin, end]
     * @return: com.sky.result.Result<com.sky.vo.TurnoverReportVO>
     * @description: 根据起止日期统计营业额
     **/
    @GetMapping("/turnoverStatistics")
    @ApiOperation("Turnover Statistics")
    public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("###### Turnover Statistics: {} ~ {}", begin, end);
        TurnoverReportVO turnoverReportVO = reportService.getTurnoverStatistics(begin, end);
        return Result.success(turnoverReportVO);
    }

    /**
     * @param: [begin, end]
     * @return: com.sky.result.Result<com.sky.vo.UserReportVO>
     * @description: 用户量和新增用户量统计
     **/
    @GetMapping("/userStatistics")
    @ApiOperation("User Statistics")
    public Result<UserReportVO> UerStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("###### User Statistics: {} ~ {}", begin, end);
        UserReportVO userReportVO = reportService.getUerStatistics(begin, end);
        return Result.success(userReportVO);
    }

    /**
     * @param: [begin, end]
     * @return: com.sky.result.Result<com.sky.vo.OrderReportVO>
     * @description: 订单信息统计
     **/
    @GetMapping("/ordersStatistics")
    @ApiOperation("Order Statistics")
    public Result<OrderReportVO> OrderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("###### Order Statistics: {} ~ {}", begin, end);
        OrderReportVO orderReportVO = reportService.getOrderStatistics(begin, end);
        return Result.success(orderReportVO);
    }

    /**
     * @param: [begin, end]
     * @return: com.sky.result.Result<com.sky.vo.SalesTop10ReportVO>
     * @description: 订单商品销量前十统计（dish和 setmeal）
     **/
    @GetMapping("/top10")
    @ApiOperation("Top10")
    public Result<SalesTop10ReportVO> top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("###### Top10: {} ~ {}", begin, end);
        SalesTop10ReportVO salesTop10ReportVO = reportService.getSalesTop10(begin, end);
        return Result.success(salesTop10ReportVO);
    }

    /**
     * @param: [httpServletResponse]
     * @return: void
     * @description: 导出运营数据报表
     **/
    @GetMapping("/export")
    @ApiOperation("Export file")
    public void export(HttpServletResponse httpServletResponse){
        log.info("###### Export file");
        reportService.exportBusinessData(httpServletResponse);
    }
}
